package com.xspace.android.xafcommon.db.dbsub;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.xspace.android.xafcommon.appliction.BaseApplication;
import com.xspace.android.xafcommon.db.BaseDBHelper;
import com.xspace.android.xafcommon.db.dbbiz.PreferencesDBBiz;
import com.xspace.android.xafcommon.util.ClassUtil;
import com.xspace.android.xafcommon.util.LogUtil;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


public class MyDBHelper extends BaseDBHelper {
    private static final String DB_NAME = "maf.db";
    /****
     * 34只修改OfflineHtmlDB表 35 添加 formtime flag字段 36 只修改OfflinePedingItemDB表 37
     * 增加表 OfflineDataRecordDB 38 修改OfflineHtml表结构 39 新增几个表 工作事项、服务对象、工作表单、待办事项
     * 、工作日志文件上传表
     */
    private static final int DB_VERSION = 40; // 数据库有变动的时候编号需要加1

    public MyDBHelper(Context context) {
        super(context, DB_NAME, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        //39所加
        new WorkTypeDBBiz().create(db);
        new WorkServiceObjDBBiz().create(db);
        new WorkFormDBBiz().create(db);
        new CacheBackLogDB().create(db);
        new FileUploadDBBiz().create(db);

        new QACSortDBBiz().create(db);
        new TabHostDBBiz().create(db);
        new AppDownDBBiz().create(db);
        new AppMenusDBBiz().create(db);
        new AppDBBiz().create(db);
        // new CallRecordDBBiz().create(db);
        // new TradeDBBiz().create(db);
        new PreferencesDBBiz().create(db);
        new AuditAppDBBiz().create(db);
        new MAreaDB().create(db);
        new AudioMsgDb().create(db);
        new FileMgDb().create(db);
        new DraftMsgDB().create(db);
        new OfflineObjectDB().create(db);
        new OfflineWorkLogDB().create(db);
        new OfflineHtmlDB().create(db);
        new OfflinePedingItemDB().create(db);
        new OfflineDataRecordDB().create(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        LogUtil.d("onUpgrade", "oldVersion>" + oldVersion + "newVersion>" + newVersion);
        if (newVersion > oldVersion) {

            if (newVersion >= 36) {
                switch (oldVersion) { //旧版本
                    case 35:
                        new OfflinePedingItemDB().dropTable(db);
                        new OfflinePedingItemDB().create(db);
                    case 36:
                        new OfflineDataRecordDB().dropTable(db);
                        new OfflineDataRecordDB().create(db);
                    case 37:
                        new OfflineHtmlDB().dropTable(db);
                        new OfflineHtmlDB().create(db);
                    case 38:
                        new WorkTypeDBBiz().create(db);
                        new WorkServiceObjDBBiz().create(db);
                        new WorkFormDBBiz().create(db);
                        new CacheBackLogDB().create(db);
                        new FileUploadDBBiz().create(db);
                    case 39:
                        new FileUploadDBBiz().upgrade(db);
                }
//            } else if (newVersion == 39) {
//                if (oldVersion == 38) {
//                    new WorkTypeDBBiz().create(db);
//                    new WorkServiceObjDBBiz().create(db);
//                    new WorkFormDBBiz().create(db);
//                    new CacheBackLogDB().create(db);
//                    new FileUploadDBBiz().create(db);
//                } else if (oldVersion == 37) {
//                    new WorkTypeDBBiz().create(db);
//                    new WorkServiceObjDBBiz().create(db);
//                    new WorkFormDBBiz().create(db);
//                    new CacheBackLogDB().create(db);
//                    new FileUploadDBBiz().create(db);
//
//                    new OfflineHtmlDB().dropTable(db);
//                    new OfflineHtmlDB().create(db);
//                } else if (oldVersion == 36) {
//                    new WorkTypeDBBiz().create(db);
//                    new WorkServiceObjDBBiz().create(db);
//                    new WorkFormDBBiz().create(db);
//                    new CacheBackLogDB().create(db);
//                    new FileUploadDBBiz().create(db);
//
//                    new OfflineHtmlDB().dropTable(db);
//                    new OfflineHtmlDB().create(db);
//                    new OfflineDataRecordDB().dropTable(db);
//                    new OfflineDataRecordDB().create(db);
//
//                }
//            } else if (newVersion == 38) {
//                if (oldVersion == 37) {
//                    new OfflineHtmlDB().dropTable(db);
//                    new OfflineHtmlDB().create(db);
//                }
//                if (oldVersion == 36) {
//                    new OfflineHtmlDB().dropTable(db);
//                    new OfflineHtmlDB().create(db);
//                    new OfflineDataRecordDB().dropTable(db);
//                    new OfflineDataRecordDB().create(db);
//                }
//            } else if (newVersion == 37) {
//                new OfflineDataRecordDB().dropTable(db);
//                new OfflineDataRecordDB().create(db);
//            } else if (newVersion == 36 && oldVersion == 35) {
//                new OfflinePedingItemDB().dropTable(db);
//                new OfflinePedingItemDB().create(db);
            } else if (newVersion == 34 && oldVersion == 33) {
                new OfflineHtmlDB().dropTable(db);
                new OfflineHtmlDB().create(db);
            } else {
                new QACSortDBBiz().dropTable(db);
                new TabHostDBBiz().dropTable(db);
                new AppDownDBBiz().dropTable(db);
                new AppDBBiz().dropTable(db);
                new PreferencesDBBiz().dropTable(db);
                // 数据库升级 清理用户名密码需要关闭远程service

//                ClassUtil.getMethod("", );
                Class clz = ClassUtil.getClassByName("cn.xspace.library.util.Tools");
                Object obj = null;
                if (clz != null) {
                    try {
                        Method newInstance = clz.getMethod("exitApp");
                        obj = newInstance.invoke(BaseApplication.mInstance);
                    } catch (NoSuchMethodException e) {
                        e.printStackTrace();
                    } catch (InvocationTargetException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }

//                Tools.exitApp(BaseApplication.mInstance);
                // BaseDbMg.getInstance().getAppMenusSql().deleteUnAndroidApp();
                // BaseDbMg.getInstance().getAppMenusSql().queryApp();
                new AppMenusDBBiz().dropTable(db);
                new AuditAppDBBiz().dropTable(db);
                // new TradeDBBiz().dropTable(db);
                new QACSortDBBiz().create(db);
                new TabHostDBBiz().create(db);
                new AppDownDBBiz().create(db);
                new AppMenusDBBiz().create(db);
                // new TradeDBBiz().create(db);
                new AuditAppDBBiz().create(db);
                new PreferencesDBBiz().create(db);
                new AppDBBiz().create(db);
                new MAreaDB().dropTable(db);
                new AudioMsgDb().dropTable(db);
                new FileMgDb().dropTable(db);
                new FileMgDb().create(db);
                new DraftMsgDB().dropTable(db);
                new DraftMsgDB().create(db);

                new OfflineObjectDB().dropTable(db);
                new OfflineObjectDB().create(db);
                new OfflineWorkLogDB().dropTable(db);
                new OfflineWorkLogDB().create(db);
                new OfflineHtmlDB().dropTable(db);
                new OfflineHtmlDB().create(db);
                new OfflinePedingItemDB().dropTable(db);
                new OfflinePedingItemDB().create(db);
            }
        }
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        LogUtil.d("onDowngrade", "oldVersion>" + oldVersion + "newVersion>" + newVersion);
        if (newVersion == 36 && oldVersion == 37) {
            new OfflineDataRecordDB().dropTable(db);
        }
    }

}